import axios from "@/axios";
//注册
export function register(data) {
    return axios.post("/api_1_0/administrator/register", data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
//登录
export function login(data) {
    return axios.post("/api_1_0/administrator/login", data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
//修改头像
export function modifyHeadPic(data) {
     // 从 localStorage 获取 token
     const token = localStorage.getItem('token') || '';
     // 去掉 Bearer 前缀
     const tokenWithoutBearer = token.replace('Bearer ', '');
     
    return axios.put("/administrator/modifyHeadPic", data, {
        headers: {
            'Content-Type':  'multipart/form-data',
            'Token': tokenWithoutBearer  // 使用去��� Bearer 前缀的 token
        }
    });
}
//修改密码
export function modifyPassword(data) {
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token') || '';
    // 去掉 Bearer 前缀
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    return axios.put("/api_1_0/administrator/modifyPassword", 
        {
            old_password: data.old_password,
            new_password: data.new_password
        },
        {
            headers: {
                'Content-Type':  'multipart/form-data',
                'Token': tokenWithoutBearer  // 使用去��� Bearer 前缀的 token
            }
        }
    );
}
//获取个人信息
export function me() {
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token') || '';
    // 去掉 Bearer 前缀
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    return axios.get("/api_1_0/administrator/me", {
        headers: {
            'Content-Type': 'application/json',
            'Token': tokenWithoutBearer
        }
    });
}

//获取用户列表
export function getUsers() {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    return axios.get("/api_1_0/user", {
        headers: {
            'Content-Type': 'application/json',
            'Token': tokenWithoutBearer
        }
    });
}

// 删除用户
export function deleteUser(userId) {
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token') || '';
    // 去掉 Bearer 前缀
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    return axios({
        method: 'put',
        url: `/api_1_0/administrator/delete/${userId}`,
        headers: {
            'Content-Type': 'application/json',
            'Token': tokenWithoutBearer
        }
    });
}

//获取词典列表
export function dictionary() {
    return axios.get("/api_1_0/dictionary", {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}
export function add(data) {
        // 从 localStorage 获取 token
        const token = localStorage.getItem('token') || '';
        // 去掉 Bearer 前缀
        const tokenWithoutBearer = token.replace('Bearer ', '');
    return axios.post("/api_1_0/dictionary/add", data, {
        headers: {
            'Content-Type': 'multipart/form-data',
            'Token': tokenWithoutBearer
        }
    });
}

// 删除词典的接口
export function deleteDictionary(dictionaryId) {
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token') || '';
    // 去掉 Bearer 前缀
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    return axios.put(`/api_1_0/dictionary/delete/${dictionaryId}`, null, {
        headers: {
            'Content-Type': 'application/json',
            'Token': tokenWithoutBearer
        }
    });
}

export function rename(dictionaryId, data) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');
    
    const formData = new FormData();
    formData.append('new_name', data.name);
    
    return axios.put(`/api_1_0/dictionary/rename/${dictionaryId}`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data',
            'Token': tokenWithoutBearer
        }
    });
}

// 搜索词典的接口
export function search(name) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');
    return axios.post("/api_1_0/dictionary/search", {
        "name": name
    }, {
        headers: {
            'Content-Type': 'application/json',
            'Token': tokenWithoutBearer
        }
    });
}

// 获取词典的单词
export function getwords(dictionaryId) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'get',
        url: `/api_1_0/dictionary/getwords/${dictionaryId}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

export function deleteWord(dictionaryId, wordId) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'put',
        url: `/api_1_0/dictionary/delete_word/${dictionaryId}/${wordId}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

export function settest(dictionaryId, wordId) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'post',
        url: `/api_1_0/dictionary/set_word_as_test/${dictionaryId}/${wordId}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

export function canceltest(dictionaryId, wordId) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'post',
        url: `/api_1_0/dictionary/cancel_word_as_test/${dictionaryId}/${wordId}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

// 获取文章列表
export function article() {
    return axios.get("/api_1_0/article", {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}

// 获取待审核文章

export function get_disapproved_articles() {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'get',
        url: `/api_1_0/article/get_disapproved_articles`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

// 审核文章
export function approve_article(article_id) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'put',
        url: `/api_1_0/article/approve_article/${article_id}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}

export function delete_article(article_id) {
    const token = localStorage.getItem('token') || '';
    const tokenWithoutBearer = token.replace('Bearer ', '');

    return axios({
        method: 'put',
        url: `/api_1_0/article/delete_article/${article_id}`,
        headers: {
            'Token': tokenWithoutBearer
        }
    });
}